package com.finance.action.android.customer;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.CommonConfig;
import com.common.vo.JsonVo;
import com.finance.entity.customer.Contacts;
import com.finance.service.customer.ContactsService;

@Controller("ContactsAction")
public class ContactsAction {

	@Resource(name = "ContactsService")
	private ContactsService contactsService;

	@ResponseBody
	 @RequestMapping(value = "addcontacts", method = RequestMethod.POST )
//	@RequestMapping("addcontacts")
	public JsonVo<String> addcontacts(@RequestParam("kfUserId")String kfUserId,@RequestParam("contacts")String contacts) {
		JsonVo<String> json = new JsonVo<String>();
		System.err.println(contacts);
		try {
			JSONObject jsons = JSONObject.fromObject(contacts);
			JSONArray jsonarr = jsons.getJSONArray("contacts");
			for (int i = 0; i < jsonarr.size(); i++) {
				Contacts contact = new Contacts();
				JSONObject c = (JSONObject) jsonarr.get(i);
				contact.setContactsName(c.getString("name"));
				contact.setContactsPhone(c.getString("phone"));
				contact.setContactsId(CommonConfig.createUserId());
				contact.setKfUserId(kfUserId);
				contact.setContactsCreationTime(new Date());
				contact.setContactsUpdateTime(new Date());
				contactsService.insert(contact);
			}
			json.setCode(200);
			json.setMsg("成功");
			json.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			json.setMsg("解析json失败");
			json.setCode(1);
			json.setSuccess(false);
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("findcontacts")
	public JsonVo<List<Contacts>> findJsonVo(String kfUserId){
		JsonVo<List<Contacts>> json = new JsonVo<List<Contacts>>();
		List<Contacts> list = contactsService.findByList(kfUserId);
		if (list.size() == 0) {
			json.setMsg("暂时还没得联系人");
			json.setCode(0);
			return json;
		}
		json.setContent(list);
		json.setCode(200);
		json.setSuccess(true);
		json.setMsg("成功");
		return json;
	}
}
